package Book;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

public class Book implements Comparable<Book>{
    //编号
    private int Bookid;
    //书名
    private String name;
    //作者
    private String author;
    //出版年份
    private String publisher;
    //是否被借出
    private boolean isBorrowed;
    //借阅次数
    private int borrowCount;
    //上架时间
    private LocalDate putOnShelfDate;

    public Book(String name, String author, String publisher, LocalDate putOnShelfDate) {
        this.name = name;
        this.author = author;
        this.publisher = publisher;
        this.putOnShelfDate = putOnShelfDate;
    }

    public int getBookid() {
        return Bookid;
    }

    public void setBookid(int bookid) {
        Bookid = bookid;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAuthor() {
        return author;
    }

    public void setAuthor(String author) {
        this.author = author;
    }

    public String getPublisher() {
        return publisher;
    }

    public void setPublisher(String publisher) {
        this.publisher = publisher;
    }

    public boolean isBorrowed() {
        return isBorrowed;
    }

    public void setBorrowed(boolean borrowed) {
        isBorrowed = borrowed;
    }

    public int getBorrowCount() {
        return borrowCount;
    }

    public void setBorrowCount(int borrowCount) {
        this.borrowCount = borrowCount;
    }

    public LocalDate getPutOnShelfDate() {
        return putOnShelfDate;
    }

    public void setPutOnShelfDate(LocalDate putOnShelfDate) {
        this.putOnShelfDate = putOnShelfDate;
    }
    //对借阅次数进行自增/自减
    public void incrementBorrowCount() {
        this.borrowCount++;
    }
    public void decrementBorrowCount() {
        this.borrowCount--;
    }

    @Override
    public String toString() {
        return "Book{" +
                "Bookid=" + Bookid +
                ", name='" + name + '\'' +
                ", author='" + author + '\'' +
                ", publisher='" + publisher + '\'' +
                ", isBorrowed=" + isBorrowed +
                ", borrowCount=" + borrowCount +
                ", putOnShelfDate=" + putOnShelfDate +
                '}';
    }

    @Override
    public int compareTo(Book o) {
        return o.getBorrowCount() - this.getBorrowCount();
    }
    public String toJson() {
        StringBuilder json = new StringBuilder();
        json.append(Bookid).append(",");
        json.append(name).append(",");
        json.append(author).append(",");
        json.append(publisher).append(",");
        json.append(isBorrowed).append(",");
        json.append(borrowCount).append(",");
        json.append(putOnShelfDate != null ? putOnShelfDate.format(DateTimeFormatter.ISO_LOCAL_DATE) : "null");
        return json.toString();
    }


}
